package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirAirportMapper;
import zhongbo.java.air.dictionary.model.AirAirport;
import zhongbo.java.air.dictionary.service.IAirAirportService;

import java.util.Date;

@Service
public class AirAirportImpl implements IAirAirportService<AirAirport> {

    @Autowired
    private AirAirportMapper airAirportMapper;

    @Override
    public AirAirport queryById(String id) {
        AirAirport airAirport = airAirportMapper.selectById(id);
        if (airAirport == null){
            throw new RuntimeException("查询失败！当前id：" + id);
        }
        return airAirport;
    }

    @Override
    public PageVo<AirAirport> queryByPage(Integer pageNumber, Integer pageSize, String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirAirport> queryWrapper  = new QueryWrapper<>();
        //支持指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        //支持指定字段排序
        if (StrUtil.isNotBlank(oderByField) && orderByAsc){
            queryWrapper.orderByAsc(oderByField);
        }

        IPage<AirAirport> pageParam = new Page<>(pageNumber,pageSize);
        IPage<AirAirport> airAirportIPage = airAirportMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(airAirportIPage);
    }

    @Override
    public AirAirport insert(AirAirport airAirport) {
        if (airAirportMapper.exists(new QueryWrapper<AirAirport>().eq("icao_code", airAirport.getIcaoCode()))){
            throw new RuntimeException("ICAO机场代码已存在");
        }
        if (airAirportMapper.exists(new QueryWrapper<AirAirport>().eq("iata_code", airAirport.getIataCode()))){
            throw new RuntimeException("IATA机场代码已存在");
        }
        if (airAirportMapper.exists(new QueryWrapper<AirAirport>().eq("name", airAirport.getName()))){
            throw new RuntimeException("机场全称已存在");
        }
        airAirport.setCreateTime(new Date());
        airAirport.setUpdateTime(new Date());
        int insert = airAirportMapper.insert(airAirport);
        if (insert <= 0){
            throw new RuntimeException("添加失败 ->>> " + airAirport);
        }
        return airAirport;
    }

    @Override
    public AirAirport update(AirAirport airAirport) {
        if (airAirportMapper.exists(new QueryWrapper<AirAirport>().eq("icao_code", airAirport.getIcaoCode()).ne("id",airAirport.getId()))){
            throw new RuntimeException("ICAO机场代码已存在");
        }
        if (airAirportMapper.exists(new QueryWrapper<AirAirport>().eq("iata_code", airAirport.getIataCode()).ne("id",airAirport.getId()))){
            throw new RuntimeException("IATA机场代码已存在");
        }
        if (airAirportMapper.exists(new QueryWrapper<AirAirport>().eq("name", airAirport.getName()).ne("id",airAirport.getId()))){
            throw new RuntimeException("机场全称已存在");
        }
        airAirport.setUpdateTime(new Date());
        int i = airAirportMapper.updateById(airAirport);
        if (i <= 0){
            throw new RuntimeException("修改失败 ->>> id：" + airAirport.getId() + " 不存在; 实体信息 ->>> " + airAirport);
        }
        return airAirport;
    }

    @Override
    public void deleteById(String id) {
        int i = airAirportMapper.deleteById(id);
        if (i <= 0){
            throw new RuntimeException("删除失败 ->>> id：" + id);
        }
    }
}
